﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;

namespace Election.BanToChucBinhBau
{
	public partial class Form_BanTCBB_Main : Form
	{
		public Form_BanTCBB_Main()
		{
			InitializeComponent();
		}

		private void Form_BanTCBB_Main_Load(object sender, EventArgs e)
		{
			toolStripButton1.PerformClick();
			toolStripButton7.PerformClick();
		}

		private OracleDataAdapter adapter;
		private DataTable table;
		private DataTable table2;

		private void toolStripButton1_Click(object sender, EventArgs e)
		{
			table = new DataTable();
			table.Columns.Add("MaUCV");
			table.Columns.Add("MaDotBauCu");
			table.Columns.Add("MaTV");
			table.Columns.Add("QuaTrinhCongTac");
			OracleCommand comm = new OracleCommand();
			comm.Connection = orcl.conn;
			comm.CommandType = CommandType.StoredProcedure;
			comm.CommandText = "QUANLYBAUCU.PROC_BANTOCHUC_UCV";
			comm.Parameters.Add("p_cursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
			try
			{
				OracleDataReader reader = comm.ExecuteReader();
				while (reader.Read())
				{
					DataRow row = table.NewRow();
					row[0] = reader["MaUCV"];
					row[1] = reader["MaDotBauCu"];
					row[2] = reader["MaTV"];
					row[3] = reader["QuaTrinhCongTac"];
					table.Rows.Add(row);
				}
				table.AcceptChanges();
				gridViewUCV.DataSource = table;
				reader.Close();
				reader.Dispose();
				comm.Dispose();
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void toolStripButton2_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < table.Rows.Count; i++)
			{
				if (table.Rows[i].RowState == DataRowState.Added)
				{
					try
					{
						OracleCommand comm = new OracleCommand();
						comm.Connection = orcl.conn;
						comm.CommandType = CommandType.StoredProcedure;
						comm.CommandText = "QUANLYBAUCU.PROC_BANTOCHUC_UCV_INSERT";
						comm.Parameters.Add("ucv", OracleDbType.Varchar2).Value = table.Rows[i]["MaUCV"];
						comm.Parameters.Add("dbc", OracleDbType.Varchar2).Value = table.Rows[i]["MaDotBauCu"];
						comm.Parameters.Add("tv", OracleDbType.Varchar2).Value = table.Rows[i]["MaTV"];
						comm.Parameters.Add("quatrinh", OracleDbType.Varchar2).Value = table.Rows[i]["QuaTrinhCongTac"];
						comm.ExecuteNonQuery();
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
				if (table.Rows[i].RowState == DataRowState.Deleted)
				{
					try
					{
						OracleCommand comm = new OracleCommand();
						comm.Connection = orcl.conn;
						comm.CommandType = CommandType.StoredProcedure;
						comm.CommandText = "QUANLYBAUCU.PROC_BANTOCHUC_UCV_DELETE";
						comm.Parameters.Add("ucv", OracleDbType.Varchar2).Value = table.Rows[i]["MaUCV", DataRowVersion.Original].ToString();
						comm.ExecuteNonQuery();
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
				if (table.Rows[i].RowState == DataRowState.Modified)
				{
					try
					{
						OracleCommand comm = new OracleCommand();
						comm.Connection = orcl.conn;
						comm.CommandType = CommandType.StoredProcedure;
						comm.CommandText = "QUANLYBAUCU.PROC_BANTOCHUC_UCV_UPDATE";
						comm.Parameters.Add("ucv", OracleDbType.Varchar2).Value = table.Rows[i]["MaUCV"];
						comm.Parameters.Add("dbc", OracleDbType.Varchar2).Value = table.Rows[i]["MaDotBauCu"];
						comm.Parameters.Add("tv", OracleDbType.Varchar2).Value = table.Rows[i]["MaTV"];
						comm.Parameters.Add("quatrinh", OracleDbType.Varchar2).Value = table.Rows[i]["QuaTrinhCongTac"];
						comm.ExecuteNonQuery();
					}
					catch (Exception ex)
					{
						MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
		}

		private void toolStripButton3_Click(object sender, EventArgs e)
		{
			string ucv = gridViewUCV.CurrentRow.Cells["MaUCV"].Value.ToString();
			Election.BanToChucBinhBau.Form_BanTCBB_UploadImg form = new Form_BanTCBB_UploadImg(ucv);
			form.ShowDialog();
		}

		private void toolStripButton7_Click(object sender, EventArgs e)
		{
			try
			{
				OracleCommand comm = new OracleCommand();
				comm.Connection = orcl.conn;
				comm.CommandText = "select * from QUANLYBAUCU.THANHVIEN";
				adapter = new OracleDataAdapter();
				adapter.SelectCommand = comm;
				table2 = new DataTable();
				adapter.Fill(table2);
				//table2.Columns["MaTB"].ReadOnly = true;
				gridViewToBan.DataSource = table2;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void toolStripButton8_Click(object sender, EventArgs e)
		{
			try
			{
				OracleCommandBuilder builder = new OracleCommandBuilder(adapter);
				adapter.Update(table2);
				MessageBox.Show("Cập nhật dữ liệu thành công", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void toolStripButton9_Click(object sender, EventArgs e)
		{
			ThemUngCuVienForm form = new ThemUngCuVienForm();
			form.ShowDialog();
		}


	}
}
